import basePlay from "./base/basePlay";
import { menu } from "../../decorator";

const { ccclass, property, requireComponent } = cc._decorator;

@ccclass
@menu(__filename)
@requireComponent(sp.Skeleton)
export default class playSpine extends basePlay {
    protected _needPreview: boolean = false;

    @property
    private __animation: string = '<None>';
    @property({
        visible: false
    })
    get animation() { return this.__animation; }
    set animation(value) {
        this.__animation = value;
        if (CC_EDITOR) {
        } else {
            this._play();
        }
    }

    @property({
        tooltip: CC_DEV && '在编辑器中不可编辑,跟随Skeleton中的Animation属性\n运行中可以通过代码设置',
        visible: true
    })
    get _animation() {
        return this.__animation;
    }

    static Event = {
        spinePlay: 'spinePlay',
        spineStop: 'spineStop',
        spinePause: 'spinePause',
        spineResume: 'spineResume',
        spineComplete: 'spineComplete',

        spineInterrupt: 'spineInterrupt',
        spineDispose: 'spineDispose',
        spineEvent: 'spineEvent'
    }

    // spine组件
    private _currSpine: sp.Skeleton = null;
    // 是否完成
    private _isCompelte = false;
    // 完成控制变量
    private _compelteCtr = false;
    // 完成回调
    private _completeCallback: Function = null;

    onLoad() {
        const spine = this._currSpine = this.node.getComponent(sp.Skeleton);
        this._currSpine.loop = false;
        this._currSpine.paused = true;

        // 开始
        spine.setStartListener(() => {
        });
        // 结束
        spine.setEndListener(() => {
        });
        // 完成一次动画
        spine.setCompleteListener(() => {
            this._compelteCtr = true;
        });
        spine.setInterruptListener(() => {
            this.node.emit(playSpine.Event.spineInterrupt);
        });
        spine.setDisposeListener(() => {
            this.node.emit(playSpine.Event.spineDispose);
        });
        spine.setEventListener((a, b, c) => {
            this.node.emit(playSpine.Event.spineEvent, a, b, c);
        });

        super.onLoad();
    }

    protected _play(loopCount?: number): boolean {
        this.stop();

        if (!this.animation) return false;

        this._isCompelte = false;
        this._compelteCtr = false;
        this._currSpine.paused = true;
        this._currSpine.loop = false;
        this._currSpine.animation = this.animation;

        super.basePlay(0, loopCount);

        return true;
    }

    protected _beforeRender() {
        if (this._compelteCtr === false) {
            this._isCompelte = false;
        }
    }

    protected _render(dt: number): boolean {
        if (this._compelteCtr === false) {
            this._currSpine.paused = false;
            this._currSpine['update'](dt);
            this._currSpine.paused = true;
        }

        return this.complete();
    }

    /**
     * 播放动画
     * @param name 动作名字
     * @param playTimes 播放次数(默认无限循环)
     */
    play(name?: string, playTimes = -1, complete?: Function) {
        // 先停止
        this.stop();

        if (typeof name === 'string') {
            this.__animation = name;
        }
        playTimes = Math.floor(playTimes);
        this._completeCallback = complete || null;

        // 0代表不播放
        if (playTimes === 0) return false;

        // 播放失败
        if (!this._play(playTimes - 1)) return false;

        if (!CC_EDITOR) this.node.emit(playSpine.Event.spinePlay);

        return true;
    }

    /**
     * 播放一次
     * @param name 动作名字
     */
    playOnce(name?: string, complete?: Function) {
        return this.play(name, 1, complete);
    }

    private complete() {
        if (this._compelteCtr) {
            this._isCompelte = true;
            this._compelteCtr = false;

            // 完成一次动画
            if (this._completeCallback) {
                this._completeCallback(true);
            }

            if (!CC_EDITOR) this.node.emit(playSpine.Event.spineComplete);

            return true;
        }
        return false;
    }

    stop() {
        if (super.stop()) {
            this._currSpine.paused = true;

            // 完成一次动画的中途被停止了
            if (this._isCompelte === false && this._completeCallback) {
                this._completeCallback(false);
                this._completeCallback = null;
            }

            if (!CC_EDITOR) this.node.emit(playSpine.Event.spineStop);
            return true;
        }
        return false;
    }

    pause() {
        if (super.pause()) {
            this._currSpine.paused = true;
            if (!CC_EDITOR) this.node.emit(playSpine.Event.spinePause);
            return true;
        }
        return false;
    }

    resume() {
        if (super.resume()) {
            this._currSpine.paused = false;
            if (!CC_EDITOR) this.node.emit(playSpine.Event.spineResume);
            return true;
        }
        return false;
    }

    update(dt: number) {
        super.update(dt);

        if (CC_EDITOR && this._currSpine) {
            if (this._currSpine.loop) {
                //@ts-ignore
                Editor.info('不要使用sp.Skeleton的loop属性，请使用playSpine的loop属性代替');
                this._currSpine.loop = false;
            }

            this.__animation = this._currSpine.animation || '<None>';
        }
    }
}
